/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.shooter;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.InputAdapter;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.g2d.BitmapFont;

public class Game extends InputAdapter implements ApplicationListener,ITimer {
        private SpriteBatch batch;
        private Texture texture;
        private Player player;
        OrthographicCamera camera;
        private Sky sky;
        private ShapeRenderer shapeRenderer ;
       
        private ITimer timer;
        
        private Bomber bomber;
        
        private boolean isBomber = false;
        
        private Sound fireSnd;

        private Texture backGroundTexture;
        
        private BitmapFont textFont;
        
        private CollisionManager collisionManager;
        
    @Override
        public void create() {
          
            batch = new SpriteBatch();
            camera = new OrthographicCamera();
            camera.setToOrtho(false, 800, 480);
            Texture.setEnforcePotImages(false); // This will over come issue of power of 2 while loading texture
            player = new Player();
             Gdx.input.setInputProcessor(this);
            sky = new Sky();
            shapeRenderer = new ShapeRenderer();
            // initialize bomber system
            initializeBomber();
            timer = this;
            GameTimer.start(timer); //strt the AI timer
            fireSnd = Gdx.audio.newSound(Gdx.files.internal("LazerRicochet.wav"));
            textFont = new BitmapFont();
            collisionManager = new CollisionManager();
        }

    @Override
        public void render(){
       // spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
       // Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); // This cryptic line clears the screen.
       // System.out.println(" current time is " + System.currentTimeMillis()); 
          GameTimer.calculateDeltaTime(0);
          sky.moveStars();
       //   player.update(Gdx.graphics.getDeltaTime());
          player.drawPlayer(isBomber);
          player.drawExplosion();
             if(Gdx.input.isKeyPressed(Keys.LEFT)){
             player.moveLeft();    
          }else if(Gdx.input.isKeyPressed(Keys.RIGHT)){
             player.moveRight(); 
          } else if(Gdx.input.isKeyPressed(Keys.UP)){
             player.moveUp();   
          } 
             
          if(Gdx.input.isTouched(0)){
              Vector3 pos = translateScreenCoordinatesFirstFinger();
              int x1 = (int)pos.x;
              System.out.println("x1 " + x1);
              int y1 = (int)pos.y;
              if(x1>=0 && x1<30){
                  if(y1>=45){
                 player.moveRight();
                 player.drawPlayer(isBomber);  
                  }
              }else if(x1>=720 && x1<=750){
                  System.out.println(" left button");
                  if(y1>=45){
                 player.moveLeft();
                 player.drawPlayer(isBomber);  
                  }
              } 

          } else if(Gdx.input.isTouched(1)){
             Vector3 pos = translateScreenCoordinatesSeondFinger();
             int x1 = (int)pos.x;
             int y1 = (int)pos.y;
             if(x1>=0 && x1<30){
                  if(y1>=170 && y1<=200){
                    fireSnd.play();
                    player.setFireBullet(true);
                  }
              }else if(x1>=720 && x1<=750){
                  System.out.println(" left button");
                  if(y1>=170 && y1<=200){
                   fireSnd.play();
                   player.setFireBullet(true);
                  }
              } 
             
          }
          // draw a filled circle
          //drawFilledCircle();
          drawFilledCircle(720,45);
          drawFilledCircle(10,45);
          drawFilledCircle(10,200);
          drawFilledCircle(720,200);
          
          drawTextFont(720/2, 45,"L");
          drawTextFont(5, 45,"R");
          drawTextFont(720/2, 200,"F");
          drawTextFont(5, 200,"F");
          // drawackGround
          drawBackGround();
        }

    @Override
    public void resize (int width, int height) {
        
    }

    @Override
    public boolean keyDown(int i){
      //  System.out.println(" key " + i);
        if(i == 62) {
            fireSnd.play();
            player.setFireBullet(true);
        }
        return true;
    }

    @Override
    public boolean keyUp(int keycode) {
        //if(keycode == 62) player.setFireBullet(false);
        return super.keyUp(keycode);
    }
    
    
    @Override
    public void pause(){
    }

    @Override
    public void resume(){
    }

    @Override
    public void dispose(){
    }
    
    /*
     * This method trnslates screen coordinates to camera co-ordinates
     * and return a Vector3 object for first finger is touched
     */
    private Vector3 translateScreenCoordinatesFirstFinger(){
      Vector3 touchPos = new Vector3();
      touchPos.set(Gdx.input.getX(), Gdx.input.getY(), 0);
      camera.unproject(touchPos);
      return touchPos;
    }
    
     /*
     * This method trnslates screen coordinates to camera co-ordinates
     * and return a Vector3 object for second finger is touched
     */
    private Vector3 translateScreenCoordinatesSeondFinger(){
        Vector3 touchPos = new Vector3();
        touchPos.set(Gdx.input.getX(1), Gdx.input.getY(1), 0);
        camera.unproject(touchPos);
        return touchPos;
    }
    
    /*
     * Draws a filled circle on bottom right hand side
     */
    private void drawFilledCircle(){
        Gdx.gl.glEnable(GL10.GL_BLEND);
        Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
      //  Gdx.gl.glClearColor(0, 0, 0, 0); 
    //Gdx.gl.glClear( GL10.GL_COLOR_BUFFER_BIT  );
       shapeRenderer.setProjectionMatrix(camera.combined);
       shapeRenderer.begin(ShapeType.FilledCircle);
       //shapeRenderer.setColor(new Color(128, 128, 128, 0.15f));
        shapeRenderer.setColor(new Color(0, 1, 0, 0.5f));
       //shapeRenderer.filledCircle(470, 45, 30);
        shapeRenderer.filledCircle(720, 45, 15);
       shapeRenderer.end();
       batch.begin();
       textFont.draw(batch, "L", 430, 45);
       batch.end();
       Gdx.gl.glDisable(GL10.GL_BLEND);
    }

     private void drawFilledCircle(int x, int y){
       Gdx.gl.glEnable(GL10.GL_BLEND);
       Gdx.gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);
       shapeRenderer.setProjectionMatrix(camera.combined);
       shapeRenderer.begin(ShapeType.FilledCircle);
       shapeRenderer.setColor(new Color(0, 1, 0, 0.5f));
    // shapeRenderer.filledCircle(460, 45, 10);
       shapeRenderer.filledCircle(x, y, 30);
       shapeRenderer.end();
       Gdx.gl.glDisable(GL10.GL_BLEND);
    }
    
     private void drawTextFont(int x, int y,CharSequence str){
       batch.begin();
       textFont.draw(batch, str, x, y);
       batch.end();
    }
    
    private void drawBackGround(){
        
    }
    
    private void initializeBomber(){
      bomber = new Bomber(player);
    }
    
    @Override
    public void execute() {
      player.loadBomber();
      isBomber = true;  
    }
    
    public void update(){
    }
   
}
